package com.bawei.module_video_hall.adapter

import android.graphics.Color
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bawei.module_video_hall.databinding.ItemVideoHallTypeBinding
import com.bawei.module_video_hall.mvi.model.MultiTypeModelItem

/**
 *@author：lxy
 *@decription:
 *@time：2025/6/25
 */
class VideoHallTypeAdapter(
    val list: MutableList<MultiTypeModelItem>,
    val onItemClick: ((position:Int, typeId: Int) -> Unit)? = null
) : RecyclerView.Adapter<VideoHallTypeAdapter.VideoHallTypeViewHolder>() {

    private var selectedPosition = 0 // 默认选中第一个

    fun setData(data: List<MultiTypeModelItem>) {
        list.clear()
        list.addAll(data)
        selectedPosition = 0 // 重置选中位置
        notifyDataSetChanged() // 使用notifyDataSetChanged更安全
    }

    fun setSelectedPosition(position: Int) {
        val previousSelected = selectedPosition
        selectedPosition = position
        android.util.Log.d("VideoHallTypeAdapter", "setSelectedPosition: previous=$previousSelected, new=$position")
        notifyItemChanged(previousSelected)
        notifyItemChanged(selectedPosition)
    }

    inner class VideoHallTypeViewHolder(
        val mBinding: ItemVideoHallTypeBinding,
        private val onItemClick: ((position:Int, typeId: Int) -> Unit)? = null,
    ) : ViewHolder(mBinding.root) {

        init {
            mBinding.movieTypeText.setOnClickListener {
                val typeId = list[adapterPosition].id
                onItemClick?.invoke(adapterPosition,typeId)
            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoHallTypeViewHolder {
        val binding =
            ItemVideoHallTypeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return VideoHallTypeViewHolder(binding, onItemClick)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: VideoHallTypeViewHolder, position: Int) {
        val item = list[position]
        val textView = holder.mBinding.movieTypeText
        textView.text = item.typename
        // 根据选中状态设置字体大小
        if (position == selectedPosition) {
            android.util.Log.d("VideoHallTypeAdapter", "onBindViewHolder: position=$position is selected")
            textView.textSize = 18f // 选中时的大字体
            textView.setTextColor(Color.BLACK) // 可选：选中时改变颜色
            textView.setTypeface(null, Typeface.BOLD) // 可选：加粗
        } else {
            android.util.Log.d("VideoHallTypeAdapter", "onBindViewHolder: position=$position is not selected")
            textView.textSize = 14f // 正常字体大小
            textView.setTextColor(Color.GRAY) // 可选：未选中时颜色
            textView.setTypeface(null, Typeface.NORMAL) // 可选：正常字体
        }
    }



}